#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket_server
from conf import settings

#3、定义‘参数调用’类
class ArgvHandle(object):
    def __init__(self,args):
        self.args=args
        #正常思路来说，应该根据sys.argv进行各种action，去定义action函数
        #为了逻辑更严谨，在这里加入一个解析器parser,这个parser再去触发action
        self.argv_parser()
    #4、定义parser
    def argv_parser(self):
        if len(self.args)<2:
            self.argv_help()    #相当于shell里case语句的*）
        else:
            first_args=self.args[1]
            if hasattr(self,first_args):
                func=getattr(self,first_args)   #调取当前class的start等函数；self.func
                func()  #self.func()
            else:
                self.argv_help()
    #5、定义帮助信息
    def argv_help(self):
        print '''
                usage()
                start   :start ftp server
                stop    :stop ftp server
                ......
                '''
    #6、定义启动服务函数，启动多线程，写在另一个文件里，多线程的handle有很多交互
    def start(self):
        try:
            server=socket_server.SocketServer.ThreadingTCPServer((settings.host,settings.port),socket_server.MyTcpHandle)
            server.serve_forever()
        except KeyboardInterrupt:
            print ("server is going to shut down")
            server.shutdown()